********************************************************************************
********"Macroprudential Regulation, Quantitative Easing, and Bank Lending"*****
********************************************************************************

********************************************************************************
********************************************************************************
**********************************2019******************************************
********************************************************************************
********************************************************************************
global y "taeg_w199"
global regcommands ", pool(5) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

**#Bookmark (input)
clear *
import sas using $inputdirectory/taeg2019ana, case(lower)

duplicates drop data_oss cod_oa censito_crittog cntrct_id instrmnt_id, force
rename ente_segn bankid
rename censito_crittog borrowerid
format borrowerid %20.0g

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2019, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid

replace data_oss=data_oss*1000
gen xxx=dofc(data_oss)
gen time=mofd(xxx)
format time %tm
drop data_oss
keep if time>=tm(2019m3)&time<=tm(2020m2)

collapse (sum) acc taeg_acc, by(bankid borrowerid time)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2019m8, keepus(dtop66e14 dmid6633e14 exlnassets excashas)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinm2019, keepus(lncrefin)
keep if _merge!=2
drop _merge

gen taeg=100*taeg_acc/acc
winsor2 taeg if taeg!=., suffix(_w199) cuts(1 99)

run RO_tdummies 2019 1
run RO_tdintera 1 "dtop66e14 dmid6633e14 exlnassets excashas"

**#Bookmark (output - column 1)
est clear
cd $outputdirectory
reghdfe $y dT7dtop66e14 dT7dmid6633e14 dT7exlnassets dT7excashas lncrefin $regcommands
est save TAB5_C12
cd ..

keep if e(sample)==1
duplicates drop bankid, force
keep bankid
**#Bookmark (output - sample for future reference)
save $inputdirectory/INB_sampletaeg2019ana, replace


********************************************************************************
********************************************************************************
**********************************2015******************************************
********************************************************************************
********************************************************************************
global y "taeg_w199"
global regcommands ", pool(5) compact absorb(bankid borrowerid#time) vce(cluster bankid)"

**#Bookmark (input)
clear *
import sas ENTE_SEGN CENSITO_CRITTOG DATA_OSS VOCESOTVOC VALORE DURATA DURORI_TASSO TIP_ATT using $inputdirectory/taeg, case(lower)

rename ente_segn bankid
rename censito_crittog borrowerid
format borrowerid %20.0g

reshape wide valore, i(data_oss bankid borrowerid durata durori_tasso tip_att) j(vocesotvoc)

rename valore5504213 taeg
rename valore5504233 amount
replace taeg=taeg/10000

replace data_oss=data_oss*1000
gen xxx=dofc(data_oss)
gen time=qofd(xxx)
format time %tq
drop data_oss
drop xxx

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_gru2015, keepus(gru)
keep if _merge!=2
drop _merge
replace gru=bankid if gru==.
drop bankid
rename gru bankid

gen xxx=taeg*amount
collapse (sum) xxx amount, by(bankid borrowerid time)

**#Bookmark (input)
merge m:1 bankid using $inputdirectory/INB_preann2014m12, keepus(dtop85e13 exlnassets excashas)
keep if _merge==3
drop _merge

**#Bookmark (input)
merge m:1 bankid time using $inputdirectory/INB_refinq2015
keep if _merge!=2
drop _merge
recode lncrefin (.=0)

keep if time>=tq(2014q3)
keep if time<=tq(2015q2)

egen id=group(bankid borrowerid)
xtset id time

gen dT1=(time==tq(2014q3))
gen dT3=(time==tq(2015q1))
gen dT4=(time==tq(2015q2))

foreach x in 3 {
foreach y in  dtop85e13 exlnassets excashas {
gen dT`x'`y'=dT`x'*`y'
}
}

gen taeg=xxx/amount
drop xxx
winsor2 taeg if taeg!=., suffix(_w199) cuts(1 99)

**#Bookmark (output - column 2)
est clear
cd $outputdirectory
reghdfe $y dT3dtop85e13 dT3exlnassets dT3excashas lncrefin $regcommands
est save TAB5_C12, append
cd ..

keep if e(sample)==1
duplicates drop bankid, force
keep bankid
**#Bookmark (output - sample for future reference)
save $inputdirectory/INB_sampletaeg2015, replace

